//
//  USTarotCardShareViewController.m
//  us
//
//  Created by karry on 2025/5/16.
//

#import "USTarotCardShareViewController.h"
#import "USImageActivityItem.h"

@interface USTarotCardShareViewController ()

@end

@implementation USTarotCardShareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.contentView us_addShadow];
    
    self.gridView.gridColor = [UIColor mi_3_color];
    self.gridView.fillColor = UIColor.mi_1_color;
    self.imagV.layer.cornerRadius = 16;
    self.imagV.layer.masksToBounds = YES;
    self.tagV.backgroundColor = UIColor.us_redColor;
    
    UIButton *btn0 = [UIButton us_fillButtonWithTitle:@"下载"];
    [btn0 addTarget:self action:@selector(downLoadAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn0];
    
    UIButton *btn1 = [UIButton us_fillButtonWithTitle:@"分享"];
    [btn1 addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    [btn0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.height.mas_equalTo(UIButton.us_buttonDefaultHeight);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(-20);
    }];
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(btn0.mas_right).mas_offset(20);
        make.right.mas_offset(-20);
        make.width.height.centerY.mas_equalTo(btn0);
        
    }];
    
    self.lab0.text = [NSString stringWithFormat:@"%@·塔罗牌",[NSDateFormatter localizedStringFromDate:self.card.date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle]];
    
    self.lab1.text = [NSString stringWithFormat:@"%@%@",self.card.name,self.card.orientation==USTarotCardOrientationUpright?@"正位":@"逆位"];
    
    self.lab3.text = self.card.overview;
    
    self.imagV.image = self.card.cardCover;
    
    
}

- (void)downLoadAction:(UIButton *)sender{
    [USFeedbackTool impactSoft];
    
    UIImage *image = [self.contentView jk_screenshot];
    if (image) {
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"保存失败: %@", error.localizedDescription);
        [self.view makeToast:@"保存失败"];
    } else {
        NSLog(@"保存成功");
        [self.view makeToast:@"已保存到相册"];
    }
}

- (void)shareAction:(UIButton *)sender{
    [USFeedbackTool impactSoft];

    UIImage *image = [self.contentView jk_screenshot];
    if (!image) return;
    
    USImageActivityItem *item = [[USImageActivityItem alloc]initWithImage:image];
    item.title = @"每日塔罗";
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[item] applicationActivities:nil];
    activityVC.popoverPresentationController.sourceView = sender;
    [self presentViewController:activityVC animated:YES completion:nil];
    
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
